/*
 * @Author: 江舟 jijiangzhou@recycloud.cn
 * @Date: 2024-03-26 17:24:06
 * @LastEditors: 江舟 jijiangzhou@recycloud.cn
 * @LastEditTime: 2024-04-18 15:21:31
 * @FilePath: \vite-project\src\components\org\OrgForm.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Button, Empty, Form, Space, message } from 'antd';
import React, { useEffect, useState } from 'react';
import OrgFormCont from './OrgFormCont';
import { modifyOrganization } from '@/apis/org';

interface OrgFormProps {
	parentNode?: any;
	treeData?: any[];
	editFormData?: any;
	onSuccess?: () => void;
}

const OrgForm: React.FC<OrgFormProps> = ({ parentNode, treeData = [], editFormData = null, onSuccess }) => {
	const [form] = Form.useForm();
	const [loading, setLoading] = useState(false);
	useEffect(() => {
		if (editFormData) {
			form.setFieldsValue(editFormData);
		}
	}, [editFormData]);
	const onFinish = async (form: any) => {
		try {
			setLoading(true);
			await modifyOrganization({
				id: editFormData?.id,
				...form,
			});
			message.success('修改成功');
			onSuccess?.();
		} finally {
			setLoading(false);
		}
	};
	if (parentNode.parentId) {
		return (
			<Form
				form={form}
				className="w-600px"
				labelCol={{ span: 6 }}
				wrapperCol={{ span: 14 }}
				onFinish={onFinish}
				disabled={parentNode.isRoot}>
				<OrgFormCont treeData={treeData} parentNode={parentNode} />
				<Form.Item wrapperCol={{ offset: 6 }}>
					<Button type="primary" htmlType="submit" loading={loading} disabled={parentNode.isRoot}>
						提交
					</Button>
				</Form.Item>
			</Form>
		);
	}
	return <Empty className="mb-24px" />;
};

export default OrgForm;
